# 新建“exerc3”Python文件完成作业:使用jieba对“三国演义”中的词频进行统计并输出。
import jieba


def main(s):
    word = {}

    txt = path("doc\三国.txt")
    words = cutWord(txt)

    for i in words:
        if len(i) == 1:
            continue
        word[i] = word.get(i, 0) + 1

    # print(list(word.items()))
    lists = list(word.items())
    lists.sort(key=lambda x: x[1], reverse=True)
    print(lists[:s])


# 获取路径
def path(path):
    return open(path, 'r', encoding="utf-8").read()


# 分词
def cutWord(txt):
    return jieba.lcut(txt)


if __name__ == "__main__":
    main(10)
